import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class JrAppBar extends StatelessWidget implements PreferredSizeWidget {
  final dynamic title;
  final List<Widget>? actions;
  final Widget? leading;
  final Widget? bottom;
  final bool centerTitle;
  final Color? titleColor;
  final double? elevation;
  final double titleSpacing;
  final bool automaticallyImplyLeading;
  final Brightness? brightness;
  final IconThemeData? iconTheme;
  final TextTheme? textTheme;
  final double toolbarOpacity;
  final double bottomOpacity;
  final bool mainViewStyle;
  final Color? backgroundColor;
  final bool showRailIcon;

  JrAppBar(
      {super.key,
      required this.title,
      this.actions,
      this.leading,
      this.bottom,
      this.centerTitle = true,
      this.titleColor,
      this.elevation,
      this.titleSpacing = NavigationToolbar.kMiddleSpacing,
      this.automaticallyImplyLeading = true,
      this.brightness,
      this.iconTheme,
      this.textTheme,
      this.toolbarOpacity = 1.0,
      this.bottomOpacity = 1.0,
      this.mainViewStyle = false,
      this.backgroundColor,
      this.showRailIcon = true});

  @override
  Widget build(BuildContext context) {
    return AppBar(
      title: mainViewStyle ? null : Text(title as String, style: TextStyle(color: titleColor)),
      actions: actions,
      leading: leading ?? buildLeading(),
      centerTitle: centerTitle,
      backgroundColor: backgroundColor ?? Colors.white10,
      elevation: 0,
      titleSpacing: titleSpacing,
      flexibleSpace: mainViewStyle
          ? Container(
              decoration: const BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                  colors: [
                    Color(0xFF805DAB),
                    Color(0xFF9F7DDD),
                  ],
                ),
              ),
            )
          : null,
      automaticallyImplyLeading: automaticallyImplyLeading,
      brightness: brightness,
      iconTheme: iconTheme,
      textTheme: textTheme,
      toolbarOpacity: toolbarOpacity,
      bottomOpacity: bottomOpacity,
    );
  }

  @override
  final Size preferredSize = Size(375.w, 44.h);

  Widget buildLeading() {
    return GestureDetector(
      onTap: mainViewStyle ? null : () => Get.back(),
      child: Stack(
        clipBehavior: Clip.none,
        children: [
          if (showRailIcon)
            Container(
              height: 44,
              decoration:
                  const BoxDecoration(image: DecorationImage(image: AssetImage('assets/images/icon_rails.png'))),
            ),
          mainViewStyle
              ? Positioned(left: 20.w, top: 0, bottom: 0, child: title as Widget)
              : const Center(
                  child: Icon(Icons.arrow_back_ios, color: Colors.white),
                )
        ],
      ),
    );
  }
}
